package com.inspur.cloud.service.dataspace;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.inspur.cloud.configuration.AmbariConfig;
import com.inspur.cloud.entity.dataspace.dto.YarnInfo;
import com.inspur.cloud.entity.dataspace.dto.YarnTotalDTO;
import com.inspur.cloud.exception.YarnException;
import com.inspur.cloud.util.TConstants;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.*;

/**
 * 对yarn的操作
 */
@Service
public class YarnService {
    //log4j对象
    private static Logger log = LoggerFactory.getLogger(YarnService.class);
    @Autowired
    private ClusterConfigUtil clusterConfigUtil;
    @Autowired
    private HttpService httpService;
    @Autowired
    AmbariConfig ambariConfig;
    /**
     * 发送put请求新建yarn队列或者修改队列配额(与default平级的队列)
     *
     * @return 返回true，编辑队列信息成功，请去Insight平台重启Yarn
     */
    public boolean editYarnQueue(String parentQueue, String[] queues, String[] capacities) {
        boolean flag;

        Map<String, YarnInfo> oldQueueInfoMap = getOldQueueInfoMap();

        if ("root".equals(parentQueue)) {
            flag = httpService.doAmbariPut(TConstants.INSIGHT_YARN_URL, getJObject(queues, capacities, getProperties(), oldQueueInfoMap));
        } else {
            flag = httpService.doAmbariPut(TConstants.INSIGHT_YARN_URL, getLeafJObject(parentQueue, queues, capacities, getProperties(), oldQueueInfoMap));
        }
        log.info("=====flag:{}", flag);
        if (flag) {
            saveAndRestart();
        }
        return flag;
    }

    /**
     * 保存队列信息并重启resourceManager
     */
    public void saveAndRestart() {
        String url = TConstants.INSIGHT_YARN_URL + "/saveAndRestart";
//        httpService.doAmbariPut(url);
        log.info("yarn saveAndRestart##########################");
        httpService.doRestartYarn();
    }

    /**
     * 发送put请求新建yarn队列或者修改队列配额(与default平级的队列)
     *
     * @return 返回true，编辑队列信息成功，请去Insight平台重启Yarn
     */
    public boolean editYarnQueue(String parentQueue, Map<String, String> queueInfo) {
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        for (Map.Entry<String, String> entry : queueInfo.entrySet()) {
            list1.add(entry.getKey());
            list2.add(entry.getValue());
        }
        String[] queues = list1.toArray(new String[list1.size()]);
        String[] capacity = list2.toArray(new String[list2.size()]);
        return editYarnQueue(parentQueue,queues,capacity);
    }

    /**
     * 发送put请求删除yarn队列
     *
     * @param parentQueue
     * @param queues
     * @param capacities
     */
    public boolean delYarnQueue(String parentQueue, String[] queues, String[] capacities) {
        boolean flag = httpService.doAmbariPut(TConstants.INSIGHT_YARN_URL, getDelJson(parentQueue, queues, capacities, getProperties()));
        saveAndRestart();
        return flag;
    }

    /**
     * 发送put请求删除yarn队列
     *
     * @param parentQueue
     */
    public boolean delYarnQueue(String parentQueue, Map<String, String> queueInfo) {
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        for (Map.Entry<String, String> entry : queueInfo.entrySet()) {
            list1.add(entry.getKey());
            list2.add(entry.getValue());
        }
        String[] queues = list1.toArray(new String[list1.size()]);
        String[] capacity = list2.toArray(new String[list2.size()]);
        return delYarnQueue(parentQueue,queues,capacity);
    }

    /**
     * 主页显示Yarn队列的信息,真实的yarn队列信息与ResourceManager UI一致
     *
     * @param userName
     * @param yarnQueue
     * @return
     */
    public YarnInfo getQueueInfo(String userName, String yarnQueue) {
        YarnInfo yarnInfo = new YarnInfo();
        try {
            //拿到yarn的相关配置信息，去请求一个scheduler.json文件，里面封装着集群所有yarn队列信息
            Map rmIP = clusterConfigUtil.getYarnConfig();
            URL url = new URL("http://" + rmIP.get("yarn.resourcemanager.webapp.address") + "/ws/v1/cluster/scheduler");
            String schedulerInfo = httpService.doAmbariGet(url);
            JSONObject jo = JSONObject.parseObject(schedulerInfo);
            if (jo.get("scheduler") != null && jo.getJSONObject("scheduler").get("schedulerInfo") != null) {
                String info = jo.getJSONObject("scheduler").getString("schedulerInfo");
                if (userName.equals("admin")) {
                    return getQueues(info);
                } else {
                    String queues[] = yarnQueue.split("\\.");
                    String subYarnQueue = queues[queues.length - 1];
                    subYarnQueue = "\"" + subYarnQueue + "\"";
                    return getQueueInForTenantfo(info, subYarnQueue);
                }
            }
        } catch (Exception e) {
            log.error("***getQueueInfo***Error***Message=", e);
        }
        return yarnInfo;
    }

    /**
     * 获取旧的 Yarn 队列信息，即未修改之前的、Yarn 视图里的配置
     *
     * @return Map oldQueueInfoMap
     */
    public Map<String, YarnInfo> getOldQueueInfoMap() {
        Map<String, YarnInfo> yarnInfoMap = new HashMap<>(8);

        try {
            Map rmIP = clusterConfigUtil.getYarnConfig();
            URL url = new URL("http://" + rmIP.get("yarn.resourcemanager.webapp.address") + "/ws/v1/cluster/scheduler");
            String schedulerInfo = httpService.doAmbariGet(url);
            JSONObject jo = JSONObject.parseObject(schedulerInfo);

            if (jo.get("scheduler") != null && jo.getJSONObject("scheduler").get("schedulerInfo") != null) {
                String info = jo.getJSONObject("scheduler").getString("schedulerInfo");

                // 将 info 解析为 MAP
                YarnInfo yarnInfo = getQueues(info);
                String rootQueue = yarnInfo.getQueueName();
                List<YarnInfo> queueInfoList = yarnInfo.getQueue();

                yarnInfoMap.put(rootQueue, yarnInfo);

                if (queueInfoList != null && !queueInfoList.isEmpty()) {
                    for (YarnInfo queueInfo : queueInfoList) {
                        yarnInfoMap.put(queueInfo.getQueueName(), queueInfo);

                        List<YarnInfo> queueList = queueInfo.getQueue();
                        if (queueList != null && !queueList.isEmpty()) {
                            for (YarnInfo yarn : queueList) {
                                yarnInfoMap.put(yarn.getQueueName(), yarn);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("GetQueueInfo error! Message=", e);
        }

        return yarnInfoMap;
    }

    /**
     * 获取yarn队列假信息，没有重启Yarn的信息
     *
     * @return
     */
    public YarnInfo getQueueInfoFalse() {
        YarnInfo yarnInfo = new YarnInfo();
        //得到没有重启的yarn队列的信息
        JSONObject properties = getProperties();
        String[] oneQueues = properties.get("yarn.scheduler.capacity.root.queues").toString().split(",");
        yarnInfo.setQueueName("root");
        yarnInfo.setCapacity("100");
        List<YarnInfo> list = new ArrayList<>();
        for (String str : oneQueues) {
            System.out.println(str);
            YarnInfo info = new YarnInfo();
            info.setQueueName(str);
            String capacity = properties.get("yarn.scheduler.capacity.root." + str + ".capacity").toString();
            info.setCapacity(capacity);
            list.add(info);
            if (properties.get("yarn.scheduler.capacity.root." + str + ".queues") == null) {
                continue;
            }
            String[] twoQueues = properties.get("yarn.scheduler.capacity.root." + str + ".queues").toString().split(",");
            List<YarnInfo> list1 = new ArrayList<>();
            for (String str1 : twoQueues) {
                YarnInfo info1 = new YarnInfo();
                info1.setQueueName(str1);
                String capa = properties.get("yarn.scheduler.capacity.root." + str + "." + str1 + ".capacity").toString();
                info1.setCapacity(capa);
                list1.add(info1);
            }
            info.setQueue(list1);
        }
        yarnInfo.setQueue(list);
        return yarnInfo;
    }

    public YarnTotalDTO getQueueMetric() {
        YarnTotalDTO yarnTotalDTO = new YarnTotalDTO();
        try {
            //拿到yarn的相关配置信息，去请求一个scheduler.json文件，里面封装着集群所有yarn队列信息
            Map rmIP = clusterConfigUtil.getYarnConfig();
            URL url = new URL("http://" + rmIP.get("yarn.resourcemanager.webapp.address") + "/ws/v1/cluster/metrics");
            String schedulerInfo = httpService.doAmbariGet(url);
            JSONObject jo = JSONObject.parseObject(schedulerInfo);
            if (jo.get("clusterMetrics") != null) {
                long totalMB = jo.getJSONObject("clusterMetrics").getLongValue("totalMB");
                long unusedMB = jo.getJSONObject("clusterMetrics").getLongValue("availableMB");
                long usedMB = totalMB - unusedMB;
                int totalCore = jo.getJSONObject("clusterMetrics").getIntValue("totalVirtualCores");
                int unusedCore = jo.getJSONObject("clusterMetrics").getIntValue("availableVirtualCores");
                int usedCore = totalCore - unusedCore;
                yarnTotalDTO.setTotalCore(totalCore);
                yarnTotalDTO.setUnusedCore(unusedCore);
                yarnTotalDTO.setUsedCore(usedCore);
                yarnTotalDTO.setTotalMemory(totalMB);
                yarnTotalDTO.setUnusedMemory(unusedMB);
                yarnTotalDTO.setUsedMemory(usedMB);
            }
        } catch (Exception e) {
            log.error("***getQueueInfo***Error***Message=", e);
        }
        return yarnTotalDTO;
    }

    public YarnTotalDTO getQueueMetricTenant(String queueName) {
        long totalMB = 0L;
        long usedMB = 0L;
        long unusedMB = 0L;
        int totalCore = 0;
        int usedCore = 0;
        int unusedCore = 0;
        JSONObject properties = getProperties();
        double capacity = properties.getDoubleValue("yarn.scheduler.capacity.root." + queueName + ".capacity");
        double usedCapacity = getTenantQueueUsedCapacity(queueName);
        YarnTotalDTO yarnTotalDTO = getQueueMetric();
        if (usedCapacity != -1) {
            totalMB = (long) ((yarnTotalDTO.getTotalMemory() * capacity) / 100);
            usedMB = (long) ((totalMB * usedCapacity) / 100);
            unusedMB = totalMB - usedMB;
            totalCore = (int) Math.round((yarnTotalDTO.getTotalCore() * capacity) / 100);
            usedCore = (int) Math.round((totalCore * usedCapacity) / 100);
            unusedCore = totalCore - usedCore;
        }
        yarnTotalDTO.setCapacity(capacity);
        yarnTotalDTO.setTotalMemory(totalMB);
        yarnTotalDTO.setUnusedMemory(unusedMB);
        yarnTotalDTO.setUsedMemory(usedMB);
        yarnTotalDTO.setTotalCore(totalCore);
        yarnTotalDTO.setUnusedCore(unusedCore);
        yarnTotalDTO.setUsedCore(usedCore);
        return yarnTotalDTO;
    }

    /**
     * 得到租户队列的使用率
     *
     * @param queueName
     * @return
     */
    private int getTenantQueueUsedCapacity(String queueName) {
        int usedCapacity = -1;
        try {
            //拿到yarn的相关配置信息，去请求一个scheduler.json文件，里面封装着集群所有yarn队列信息
            Map rmIP = clusterConfigUtil.getYarnConfig();
            URL url = new URL("http://" + rmIP.get("yarn.resourcemanager.webapp.address") + "/ws/v1/cluster/scheduler");
            String schedulerInfo = httpService.doAmbariGet(url);
            JSONObject jo = JSONObject.parseObject(schedulerInfo);
            JSONObject info = jo.getJSONObject("scheduler").getJSONObject("schedulerInfo");
            JSONArray ja = info.getJSONObject("queues").getJSONArray("queue");
            for (int i = 0; i < ja.size(); i++) {
                JSONObject tmp = ja.getJSONObject(i);
                if (tmp.getString("queueName").equals(queueName)) {
                    usedCapacity = tmp.getIntValue("usedCapacity");
                    break;
                }
            }
            if (usedCapacity == -1) {
                log.warn("[{}]队列不存在", queueName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return usedCapacity;
    }

    /**
     * 从数据库中获取现有的yarn队列信息的JSON串
     * @return
     */
    public JSONObject getProperties() {
        JSONObject properties = null;
        try {
            String sad = clusterConfigUtil.getYarnConfig().get("properties").toString();
            properties = JSONObject.parseObject(sad);
        }catch (Exception e){
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 拼接成完整的json串创建修改删除队列
     *
     * @param properties
     * @return
     */
    public JSONObject getNewJson(JSONObject properties) {
        JSONObject jo1 = new JSONObject();
        jo1.put("type", "capacity-scheduler");
        jo1.put("properties", properties);
        JSONObject jo2 = new JSONObject();
        jo2.put("desired_config", jo1);
        JSONObject jo3 = new JSONObject();
        jo3.put("Clusters", jo2);
        return jo3;
    }
    /**
     * 得到新建队列的json对象(新建与default平级的队列)
     * @return
     */
    private JSONObject getJObject(String[] queues, String[] capacities, JSONObject properties, Map<String, YarnInfo> oldQueueInfoMap) {
        String rootQueues = StringUtils.join(queues, ",");
        System.out.println(rootQueues);
        properties.put("yarn.scheduler.capacity.root.queues", rootQueues);
        properties.put("yarn.scheduler.capacity.root.default.capacity", capacities[0]);
        for (int i = 1; i < queues.length; i++) {
            String capacity = "yarn.scheduler.capacity.root." + queues[i] + ".capacity";
            String acl_administer_queue = "yarn.scheduler.capacity.root." + queues[i] + ".acl_administer_queue";
            String acl_submit_applications = "yarn.scheduler.capacity.root." + queues[i] + ".acl_submit_applications";
            String minimum_user_limit_percent = "yarn.scheduler.capacity.root." + queues[i] + ".minimum-user-limit-percent";
            String maximum_capacity = "yarn.scheduler.capacity.root." + queues[i] + ".maximum-capacity";
            String user_limit_factor = "yarn.scheduler.capacity.root." + queues[i] + ".user-limit-factor";
            String state = "yarn.scheduler.capacity.root." + queues[i] + ".state";
            String priority = "yarn.scheduler.capacity.root." + queues[i] + ".priority";

            String defaultMaxCapacity = "100";
            String defaultUserLimitFactor = "1";
            String defaultMinimumUserLimitPercent = "100";
            String defaultState = "RUNNING";
            String defaultPriority = "0";

            YarnInfo oldYarnInfo = oldQueueInfoMap.getOrDefault(queues[i], null);
            if (oldYarnInfo != null) {
                defaultMaxCapacity = oldYarnInfo.getMaxCapacity();
                if (Float.valueOf(capacities[i])  > Float.valueOf(defaultMaxCapacity)) {
                    defaultMaxCapacity = capacities[i];
                }
                defaultUserLimitFactor = String.valueOf(oldYarnInfo.getUserLimitFactor());
                defaultMinimumUserLimitPercent = String.valueOf(oldYarnInfo.getMinimumUserLimitPercent());
                defaultState = oldYarnInfo.getState();
                defaultPriority = String.valueOf(oldYarnInfo.getPriority());
            }

            properties.put(capacity, capacities[i]);
            properties.put(acl_administer_queue, "*");
            properties.put(acl_submit_applications, "*");
            properties.put(maximum_capacity, defaultMaxCapacity);
            properties.put(minimum_user_limit_percent, defaultMinimumUserLimitPercent);
            properties.put(user_limit_factor, defaultUserLimitFactor);
            properties.put(state, defaultState);
            properties.put(priority, defaultPriority);
        }
        return getNewJson(properties);
    }

    /**
     * 得到新建队列的json对象(新建default那级之下的子队列)
     *
     * @param parentQueue
     * @param queues
     * @param capacities
     * @return
     */
    private JSONObject getLeafJObject(String parentQueue, String[] queues, String[] capacities, JSONObject properties, Map<String, YarnInfo> oldQueueInfoMap) {
        String[] rootQueues = ((String) properties.get("yarn.scheduler.capacity.root.queues")).split(",");
        //这个父队列必须存在才能继续执行
        if (Arrays.asList(rootQueues).contains(parentQueue)) {
            String sonQueues = StringUtils.join(queues, ",");
            System.out.println(sonQueues);
            properties.put("yarn.scheduler.capacity.root." + parentQueue + ".queues", sonQueues);
            if(properties.get("yarn.scheduler.capacity.root."+parentQueue+".ordering-policy")!=null){
                properties.remove("yarn.scheduler.capacity.root."+parentQueue+".ordering-policy");
            }
            for (int i = 0; i < queues.length; i++) {
                String capacity = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".capacity";
                String acl_administer_queue = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".acl_administer_queue";
                String acl_submit_applications = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".acl_submit_applications";
                String minimum_user_limit_percent = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".minimum-user-limit-percent";
                String maximum_capacity = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".maximum-capacity";
                String user_limit_factor = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".user-limit-factor";
                String state = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".state";
                String priority = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".priority";

                String defaultMaxCapacity = "100";
                String defaultUserLimitFactor = "1";
                String defaultMinimumUserLimitPercent = "100";
                String defaultState = "RUNNING";
                String defaultPriority = "0";

                YarnInfo oldYarnInfo = oldQueueInfoMap.getOrDefault(queues[i], null);
                if (oldYarnInfo != null) {
                    defaultMaxCapacity = oldYarnInfo.getMaxCapacity();
                    if (Float.valueOf(capacities[i])  > Float.valueOf(defaultMaxCapacity)) {
                        defaultMaxCapacity = capacities[i];
                    }
                    defaultUserLimitFactor = String.valueOf(oldYarnInfo.getUserLimitFactor());
                    defaultMinimumUserLimitPercent = String.valueOf(oldYarnInfo.getMinimumUserLimitPercent());
                    defaultState = oldYarnInfo.getState();
                    defaultPriority = String.valueOf(oldYarnInfo.getPriority());
                }

                properties.put(capacity, capacities[i]);
                properties.put(acl_administer_queue, "*");
                properties.put(acl_submit_applications, "*");
                properties.put(minimum_user_limit_percent, defaultMinimumUserLimitPercent);
                properties.put(maximum_capacity, defaultMaxCapacity);
                properties.put(user_limit_factor, defaultUserLimitFactor);
                properties.put(state, defaultState);
                properties.put(priority, defaultPriority);
            }
            return getNewJson(properties);
        } else {
            throw new YarnException("父队列不存在，无法创建子队列");
        }
    }

    /**
     * 得到要删除的队列名
     *
     * @return
     */
    public List<String> getDelQueue(String parentQueue, String[] queues, JSONObject properties) {
//        //得到yarn队列信息的json字符串中的properties信息
        String[] str1 = ((String) properties.get("yarn.scheduler.capacity.root.queues")).split(",");
        if (!"root".equals(parentQueue)) {
            str1 = ((String) properties.get("yarn.scheduler.capacity.root." + parentQueue + ".queues")).split(",");
        }
        List<String> list1 = Arrays.asList(str1);
        list1 = new ArrayList<>(list1);
        List<String> list2 = Arrays.asList(queues);
        list2 = new ArrayList<>(list2);
        list1.removeAll(list2);
        return list1;
    }



    /**
     * 得到要删除的队列的相关json的key
     *
     * @param queues
     * @return
     */
    private List<String> getDelkeys(String parentQueue, String[] queues, JSONObject properties) {
        //得到要删除的队列名
        List<String> delQueues = getDelQueue(parentQueue, queues, properties);
        String tmp = "yarn.scheduler.capacity.root." + parentQueue + ".";
        if ("root".equals(parentQueue)) {
            tmp = "yarn.scheduler.capacity.root.";
        }
        List<String> delKeys = new ArrayList<>();
        for (String q : delQueues) {
            for (Map.Entry<String, Object> entry : properties.entrySet()) {
                String key = entry.getKey();
                if (key.startsWith(tmp + q + ".")) {
                    System.out.println(key);
                    delKeys.add(key);
                }
            }
        }
        return delKeys;
    }

    /**
     * 删除队列相关的json
     * @param queues
     * @param capacities
     * @return
     */
    public JSONObject getDelJson(String parentQueue, String[] queues, String[] capacities, JSONObject properties) {
        Map<String, YarnInfo> oldQueueInfoMap = getOldQueueInfoMap();
        String rootQueues = StringUtils.join(queues, ",");
        System.out.println("rootQueues=========="+rootQueues);
        //得到要删除的队列的相关json的key
        List<String> delkeys = getDelkeys(parentQueue, queues, properties);
        if ("root".equals(parentQueue)) {
            log.info("============== delete json root queue===================");
            properties.put("yarn.scheduler.capacity.root.queues", rootQueues);
            properties.put("yarn.scheduler.capacity.root.default.capacity", capacities[0]);
            for (int i = 1; i < queues.length; i++) {
//                properties.put("yarn.scheduler.capacity.root." + queues[i] + ".capacity", capacities[i]);
//                properties.put("yarn.scheduler.capacity.root." + queues[i] + ".maximum-capacity", capacities[i]);

                String capacity = "yarn.scheduler.capacity.root." + queues[i] + ".capacity";
                String maximum_capacity = "yarn.scheduler.capacity.root." + queues[i] + ".maximum-capacity";
                String acl_administer_queue = "yarn.scheduler.capacity.root." + queues[i] + ".acl_administer_queue";
                String acl_submit_applications = "yarn.scheduler.capacity.root." + queues[i] + ".acl_submit_applications";
                String minimum_user_limit_percent = "yarn.scheduler.capacity.root." + queues[i] + ".minimum-user-limit-percent";
                String user_limit_factor = "yarn.scheduler.capacity.root." + queues[i] + ".user-limit-factor";
                String state = "yarn.scheduler.capacity.root." + queues[i] + ".state";
                String priority = "yarn.scheduler.capacity.root." + queues[i] + ".priority";

                String defaultMaxCapacity = "100";
                String defaultUserLimitFactor = "1";
                String defaultMinimumUserLimitPercent = "100";
                String defaultState = "RUNNING";
                String defaultPriority = "0";

                YarnInfo oldYarnInfo = oldQueueInfoMap.getOrDefault(queues[i], null);
                if (oldYarnInfo != null) {
                    defaultMaxCapacity = oldYarnInfo.getMaxCapacity();
                    if (Float.valueOf(capacities[i])  > Float.valueOf(defaultMaxCapacity)) {
                        defaultMaxCapacity = capacities[i];
                    }
                    defaultUserLimitFactor = String.valueOf(oldYarnInfo.getUserLimitFactor());
                    defaultMinimumUserLimitPercent = String.valueOf(oldYarnInfo.getMinimumUserLimitPercent());
                    defaultState = oldYarnInfo.getState();
                    defaultPriority = String.valueOf(oldYarnInfo.getPriority());
                }

                properties.put(capacity, capacities[i]);
                properties.put(acl_administer_queue, "*");
                properties.put(acl_submit_applications, "*");
                properties.put(maximum_capacity, defaultMaxCapacity);
                properties.put(minimum_user_limit_percent, defaultMinimumUserLimitPercent);
                properties.put(user_limit_factor, defaultUserLimitFactor);
                properties.put(state, defaultState);
                properties.put(priority, defaultPriority);
            }
        } else {
            log.info("============== delete json parent queue===================");
            properties.put("yarn.scheduler.capacity.root." + parentQueue + ".queues", rootQueues);

            for (int i = 0; i < queues.length; i++) {
//                properties.put("yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".capacity", capacities[i]);
                String capacity = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".capacity";
                String acl_administer_queue = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".acl_administer_queue";
                String acl_submit_applications = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".acl_submit_applications";
                String minimum_user_limit_percent = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".minimum-user-limit-percent";
                String maximum_capacity = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".maximum-capacity";
                String user_limit_factor = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".user-limit-factor";
                String state = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".state";
                String priority = "yarn.scheduler.capacity.root." + parentQueue + "." + queues[i] + ".priority";

                String defaultMaxCapacity = "100";
                String defaultUserLimitFactor = "1";
                String defaultMinimumUserLimitPercent = "100";
                String defaultState = "RUNNING";
                String defaultPriority = "0";

                YarnInfo oldYarnInfo = oldQueueInfoMap.getOrDefault(queues[i], null);
                if (oldYarnInfo != null) {
                    defaultMaxCapacity = oldYarnInfo.getMaxCapacity();
                    if (Float.valueOf(capacities[i])  > Float.valueOf(defaultMaxCapacity)) {
                        defaultMaxCapacity = capacities[i];
                    }
                    defaultUserLimitFactor = String.valueOf(oldYarnInfo.getUserLimitFactor());
                    defaultMinimumUserLimitPercent = String.valueOf(oldYarnInfo.getMinimumUserLimitPercent());
                    defaultState = oldYarnInfo.getState();
                    defaultPriority = String.valueOf(oldYarnInfo.getPriority());
                }

                properties.put(capacity, capacities[i]);
                properties.put(acl_administer_queue, "*");
                properties.put(acl_submit_applications, "*");
                properties.put(minimum_user_limit_percent, defaultMinimumUserLimitPercent);
                properties.put(maximum_capacity, defaultMaxCapacity);
                properties.put(user_limit_factor, defaultUserLimitFactor);
                properties.put(state, defaultState);
                properties.put(priority, defaultPriority);
            }
            if(queues.length==1 && StringUtils.isEmpty(queues[queues.length-1])){
                log.info("============== delete empty parent queue===================");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + ".queues");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".capacity");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".acl_administer_queue");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".acl_submit_applications");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".minimum-user-limit-percent");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".maximum-capacity");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".user-limit-factor");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".state");
                properties.remove("yarn.scheduler.capacity.root." + parentQueue + "." + queues[queues.length-1] + ".priority");
            }
        }
        //清除要删除队列的json
        for (String dk : delkeys) {
            properties.remove(dk);
        }
        return getNewJson(properties);
    }

    /**
     * 管理员获取所有Yarn队列信息
     * @param info
     * @return
     */
    private YarnInfo getQueues(String info) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        YarnInfo yarnInfo = new YarnInfo();
        JSONObject jo = JSONObject.parseObject(info);
        yarnInfo.setQueueName(jo.getString("queueName"));
        yarnInfo.setCapacity(String.format("%.0f",Double.valueOf(jo.getString("capacity"))));
        yarnInfo.setMaxCapacity(String.format("%.0f",Double.valueOf(jo.getString("maxCapacity"))));
        yarnInfo.setUserLimitFactor(jo.getInteger("userLimitFactor"));
        yarnInfo.setPriority(jo.getInteger("defaultPriority"));
        yarnInfo.setMinimumUserLimitPercent(jo.getInteger("userLimit"));

        if(jo.get("state") != null) {
            yarnInfo.setState(jo.getString("state"));
        } else if(jo.getString("queueName").equals("root")) {
            yarnInfo.setState("RUNNING");
        }
        if(jo.get("queues") != null) {
            List<YarnInfo> list = new ArrayList<>();
            JSONArray ja =  jo.getJSONObject("queues").getJSONArray("queue");
            for (int i = 0; i < ja.size(); i++) {
                YarnInfo yy = getQueues(ja.getJSONObject(i).toString());
                list.add(yy);
            }
            yarnInfo.setQueue(list);
        }
        return yarnInfo;
    }

    /**
     * 租户获取属于它自己的Yarn队列信息
     * @param info
     * @param yarnQueue
     * @return
     */
    private YarnInfo getQueueInForTenantfo(String info,String yarnQueue) {
        YarnInfo yarnInfo = new YarnInfo();
        if (info.contains(yarnQueue)) {
            JSONObject jo = JSONObject.parseObject(info);
            yarnInfo.setQueueName(jo.getString("queueName"));
            yarnInfo.setCapacity(String.format("%.0f", Double.valueOf(jo.getString("capacity"))));
            if (jo.get("state") == null) {
                yarnInfo.setState("RUNNING");
            } else {
                yarnInfo.setState(jo.getString("state"));
            }
            if (jo.get("queues") != null) {
                List<YarnInfo> list = new ArrayList<>();
                JSONArray ja = jo.getJSONObject("queues").getJSONArray("queue");
                YarnInfo yy = new YarnInfo();
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject jsonObject = ja.getJSONObject(i);
                    if (yarnQueue.equals("\"" + jsonObject.getString("queueName") + "\"")) {
                        yy = getQueues(ja.getJSONObject(i).toString());
                    } else {
                        yy = getQueueInForTenantfo(ja.getJSONObject(i).toString(), yarnQueue);
                    }
                    if (yy != null) {
                        list.add(yy);
                    }
                }
                if (list.size() != 0) {
                    yarnInfo.setQueue(list);
                }
            }
            return yarnInfo;
        } else {
            return null;
        }
    }

}
